import 'package:flutter/material.dart';
import 'package:flutter/animation.dart';

class AnimationPage extends StatefulWidget {
  @override
  _AnimationState createState() => _AnimationState();
}

class _AnimationState extends State<AnimationPage>
    with SingleTickerProviderStateMixin {
  Animation<double> animation;
  AnimationController controller;

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();

    controller = new AnimationController(
        duration: const Duration(milliseconds: 2000), vsync: this);

    // animation = new Tween(begin: 50.0, end: 200.0).animate(controller)
    //   ..addStatusListener((status) {
    //     print('$status');
    //     if (status == AnimationStatus.completed) {
    //       controller.reverse();
    //     } else if (status == AnimationStatus.dismissed) {
    //       controller.forward();
    //     }
    //   });

    final CurvedAnimation curve =
        new CurvedAnimation(parent: controller, curve: Curves.easeIn);
    animation = new Tween(begin: 0.0, end: 300.0).animate(curve);

    controller.forward();
  }

  @override
  Widget build(BuildContext context) {
    return new GrowTransition(child: new LogoWidget(), animation: animation);
    // new Center(
    //     child: new AnimatedLogo(
    //   animation: animation,
    // )
    // new Container(
    //     margin: new EdgeInsets.symmetric(vertical: 10.0),
    //     width: animation.value,
    //     height: animation.value,
    //     color: Colors.red),
    // );
  }
}

class AnimatedLogo extends AnimatedWidget {
  AnimatedLogo({Key key, Animation<double> animation})
      : super(key: key, listenable: animation);

  @override
  Widget build(BuildContext context) {
    final Animation<double> animation = listenable;

    return new Center(
      child: new Container(
        margin: new EdgeInsets.all(8.0),
        width: animation.value,
        height: animation.value,
        child: new FlutterLogo(),
      ),
    );
  }
}

class LogoWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Container(
      margin: new EdgeInsets.symmetric(vertical: 10.0),
      child: new FlutterLogo(),
    );
  }
}

class GrowTransition extends StatelessWidget {
  GrowTransition({this.child, this.animation});

  final Animation<double> animation;
  final Widget child;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: new AnimatedBuilder(
        animation: animation,
        builder: (BuildContext context, Widget child) {
          return new Container(
            height: animation.value,
            width: animation.value,
            child: child,
          );
        },
        child: child,
      ),
    );
  }
}
